﻿layui.use(['element', 'layer', 'util', 'jquery'], function () {
    var element = layui.element;
    var layer = layui.layer;
    var util = layui.util;
    var $ = layui.jquery;
    var token = localStorage.getItem("token");
   
    $.ajax({
        url: '../User/MenuListTreeDo',
        type: "get",
        data: { token: token },
        success: function (res) {
            res = JSON.parse(res);
            if (res.code == 200) {
                var selectData = res.data;
                var menuHtml = '';
                selectData.forEach(function (item) {
                    menuHtml += '<li class="layui-nav-item"><a href="#">' + item.label + '</a>';
                    if (item.children && item.children.length > 0) {
                        menuHtml += '<dl class="layui-nav-child">';
                        item.children.forEach(function (subItem) {
                            menuHtml += '<dd><a href="' + subItem.menu_url + '">' + subItem.label + '</a></dd>';
                        });
                        menuHtml += '</dl>';
                    }
                    menuHtml += '</li>';
                });
                $('#dynamicMenu').html(menuHtml);
                element.init();
                 $(function () {
                    var parentIndex = localStorage.getItem("ParentMenu");
                    var childIndex = localStorage.getItem("ChildMenu");
                    if (parentIndex !== null && childIndex !== null) {
                        var parentMenuItem = $('.layui-nav-item').eq(parseInt(parentIndex));
                        console.log(parentMenuItem);
                        parentMenuItem.addClass('layui-nav-itemed');
                        var childMenuItem = parentMenuItem.find('.layui-nav-child dd').eq(parseInt(childIndex));
                        childMenuItem.addClass('layui-this');
                        layui.use(['element'], function () {
                            var element = layui.element;
                            element.render('nav');
                        });
                    }
                })

                element.on('nav(demo-filter-nav)', function (elem) {
                    var parentMenuItem = $(this).parents('.layui-nav-item');
                    var parentIndex = parentMenuItem.index();
                    var childIndex = $(this).parent().index();
                    //alert(parentMenuItem);
                    //alert(parentIndex);
                    //alert(childIndex);
                    localStorage.setItem("ParentMenu", parentIndex);
                    localStorage.setItem("ChildMenu", childIndex);
                    element.tabChange('demo-filter-nav', $(elem).attr('href'));
                    var currentPageUrl = window.location.href;
                });



            } else {
                alert(res.message);
            }
        },
        error: function (err) {
            console.log(err);
        }
    });

   


    window.onload = function () {
        var username = localStorage.getItem("UserName");
        if (username) {
            document.getElementById('username').textContent = '欢迎您, ' + username;
        }
    }
});